﻿
// Global Variables.
var ucSearchResultsList = null;
var initialLoadHandler = null;

(function($) {

    window["load_BingMapsSample_ucSearchResultsList"] = function(obj) {
        
        
        // Variables.
        ucSearchResultsList = obj;
        
        
        // Event Handlers.
        $(".pager a.prev_page").click(function(event) {
            event.preventDefault();

            obj.ShowPreviousPage(parseInt($(".pager .current").text()), {
                autoUpdateHtml: true,
                onSuccess: function() { ucSearchResultsList.updateMap(); }
            });
        });

        $(".pager a.first_page").click(function(event) {
            event.preventDefault();

            obj.ShowFirstPage({
                autoUpdateHtml: true,
                onSuccess: function() { ucSearchResultsList.updateMap(); }
            });
        });

        $(".pager a.next_page").click(function(event) {
            event.preventDefault();

            obj.ShowNextPage(parseInt($(".pager .current").text()), {
                autoUpdateHtml: true,
                onSuccess: function() { ucSearchResultsList.updateMap(); }
            });
        });

        $(".pager a.last_page").click(function(event) {
            event.preventDefault();

            obj.ShowLastPage({
                autoUpdateHtml: true,
                onSuccess: function(result) { ucSearchResultsList.updateMap(); }
            });
        });

        $(".pager a.numeric_page").click(function(event) {
            event.preventDefault();

            obj.ShowPage(parseInt($(this).text()), {
                autoUpdateHtml: true,
                onSuccess: function() { ucSearchResultsList.updateMap(); }
            });
        });

        $(".pager a.next_previous").click(function(event) {
            var isPrev = null;
            var pageNum = null;

            var prev = $(this).prev();
            if (prev.length > 0) {
                if (prev.hasClass("prev_page") && prev[0].tagName === "A") {
                    isPrev = true;
                }
            }

            var next = $(this).next();
            if (next.length > 0 && (isPrev == null || !isPrev)) {
                if (next.hasClass("next_page") && next[0].tagName === "A") {
                    isPrev = false;
                }
            }

            if (isPrev != null && isPrev === true && next.length > 0 && (next.hasClass("numeric_page") || next.hasClass("current"))) {
                pageNum = parseInt(next.text()) - 1;
            }
            else if (isPrev != null && isPrev === false && prev.length > 0 && (prev.hasClass("numeric_page") || prev.hasClass("current"))) {
                pageNum = parseInt(prev.text()) + 1;
            }

            if (pageNum != null) {
                event.preventDefault();

                obj.ShowPage(pageNum, {
                    autoUpdateHtml: true,
                    onSuccess: function() { ucSearchResultsList.updateMap(); }
                });
            }
        });
        
        
        
        // Functions.
        ucSearchResultsList.updateMap = function() {
            if (ucSearchResultsMap && ucSearchResultsList.getState()["Places"])
            {
                ucSearchResultsMap.clearPlaces();
                ucSearchResultsMap.addPlaces(ucSearchResultsList.getState()["Places"]);
                
                delete ucSearchResultsList.getState().Places;
            }
        };
        
        
        
        initialLoadHandler = function() {
            ucSearchResultsList.updateMap();
        }
        
    };

})(jQuery.noConflict());